home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1997 November / Macworld (1997-11).dmg / Shareware World / Comms & Internet / BetterTelnet / documentation / Goodies / icrtest.c next >
C/C++ Source or Header  |  1997-07-08  |  5KB  |  165 lines

  1. /*   icrtest
  2. *  Produces a test pattern on an ICR compatible display. Demonstrates and provides
  3. *  example code for writing ICR programs.
  4. *
  5. *  National Center for Supercomputing Applications
  6. *  University of Illinois, Urbana-Champaign
  7. *
  8. *  by Tim Krauskopf
  9. *  This program is in the public domain.
  10. */
  11. #include <stdio.h>
  12. int
  13.        xdim=0,ydim=0;                            /* size of image on disk */
  14. char
  15.        *malloc(),
  16.        *testimage,
  17.        rgb[768];                                 /* storage for a palette */
  18. main(argc,argv)
  19.        int argc;
  20.        char *argv[];
  21.        {
  22.        register int i,j;
  23.        register char *p;
  24.        puts("Creating test pattern");
  25.        xdim = 150;
  26.        ydim = 100;
  27.        if (NULL == (testimage = malloc(xdim*ydim)))
  28.               exit(1);
  29. /*
  30. *  Make the test image in a strange pattern.
  31. */
  32.        p = testimage;
  33.        for (i=0; i<ydim; i++)
  34.               for (j=0; j<xdim; j++) {
  35.                      *p++ = 50 + (((i & 0xfffffff8) * (j & 7))>>2);
  36.        }
  37.        puts("Displaying test pattern with the Interactive Color Raster protocol");
  38.        rimage(0);                         /* display remote image with [palette] */
  39. }
  40. /*****************************************************************************/
  41. /*  rimage
  42. *  Remote display of the image using the ICR.
  43. *  Just print the codes to stdout using the protocol.
  44. */
  45. rimage(usepal)
  46.        int usepal;
  47.        {
  48.        int i,j,newxsize;
  49.        char *space,*thisline,*thischar;
  50.        register unsigned char c;
  51.  
  52. /*
  53. *  Open the window with the W command.
  54. */
  55. (void)printf("\033^W;%d;%d;%d;%d;0;test window^",0,0,xdim,ydim);
  56. /*
  57. *  If a palette should be used, send it with the M command.
  58. */
  59.        if (usepal) {
  60.               (void)printf("\033^M;0;256;768;test window^");        /* start map */
  61.               thischar = rgb;
  62.               for (j=0; j<768; j++) {
  63.                      c = *thischar++;
  64.                      if (c > 31 && c < 123) {
  65.                             putchar(c);
  66.                      }
  67.                      else {
  68.                             putchar((c>>6)+123);
  69.                             putchar((c & 0x3f) + 32);
  70.                      }
  71.               }
  72.        }
  73. /*
  74. *  Send the data for the image with RLE encoding for efficiency.
  75. *  Encode each line and send it.
  76. */
  77.        space = malloc(ydim+100);
  78.        thisline = testimage;
  79.     for (i = 0; i < ydim; i++) {
  80.        newxsize = rleit(thisline,space,xdim);
  81.               thisline += xdim;                  /* increment to next line */
  82.         (void)printf("\033^R;0;%d;%d;%d;test window^",i,1,newxsize);
  83.         thischar = space;
  84.         for (j = 0; j < newxsize; j++) {
  85. /***********************************************************************/
  86. /*  Encoding of bytes:
  87. *
  88. *  123 precedes #s 0-63
  89. *  124 precedes #s 64-127
  90. *  125 precedes #s 128-191
  91. *  126 precedes #s 192-255
  92. *  overall:  realchar = (specialchar - 123)*64 + (char-32)
  93. *            specialchar = r div 64 + 123
  94. *            char = r mod 64 + 32
  95. */
  96. /***********************************************************************/
  97.                      c = *thischar++;            /* get byte to send */
  98.                      if (c > 31 && c < 123) {
  99.                             putchar(c);
  100.                      }
  101.                      else {
  102.                             putchar((c>>6)+123);
  103.                             putchar((c & 0x3f) + 32);
  104.                      }
  105.               }
  106.        }
  107.        free(space);
  108. }
  109. /********************************************************************/
  110. /*  rleit
  111. *
  112. *  Compress the data to go out with a simple run-length encoded scheme.
  113. *
  114. */
  115. rleit(buf,bufto,len)
  116.        int len;
  117.        char *buf,*bufto;
  118.        {
  119.        register char *p,*q,*cfoll,*clead;
  120.        char *begp;
  121.        int i;
  122.        p = buf;
  123.        cfoll = bufto;                            /* place to copy to */
  124.        clead = cfoll + 1;
  125.        begp = p;
  126.        while (len > 0) {                         /* encode stuff until gone */
  127.               q = p + 1;
  128.               i = len-1;
  129.               while (*p == *q && i+120 > len && i) {
  130.                      q++;
  131.                      i--;
  132.               }
  133.               if (q > p + 2) {                   /* three in a row */
  134.                      if (p > begp) {
  135.                             *cfoll = p - begp;
  136.                             cfoll = clead;
  137.                      }
  138.                      *cfoll++ = 128 | (q-p);     /* len of seq */
  139.                      *cfoll++ = *p;              /* char of seq */
  140.                      len -= q-p;                 /* subtract len of seq */
  141.                     p = q;
  142.                      clead = cfoll+1;
  143.                      begp = p;
  144.               }
  145.               else {
  146.                      *clead++ = *p++;            /* copy one char */
  147.                      len--;
  148.                      if (p > begp + 120) {
  149.                             *cfoll = p - begp;
  150.                             cfoll = clead++;
  151.                             begp = p;
  152.                      }
  153.               }
  154.         }
  155. /*
  156. *  fill in last bytecount
  157. */
  158.        if (p > begp)
  159.               *cfoll = 128 | (p - begp);
  160.        else
  161.               clead--;                           /* dont need count position */
  162.  
  163.        return((int)(clead - bufto));             /* how many stored as encoded */
  164. }
  165.